package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import io.swagger.v3.oas.annotations.media.Schema;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class IItemServiceImpl implements IItemService {

    @Autowired
    RemoteProductService remoteProductService;

    @Override
    public ItemVo getItemBySkuId(Long skuId) {

        ItemVo itemVo = new ItemVo();

        //远程调用1：skuid获取sku信息
        R<ProductSku> productSkuR = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
        ProductSku productSku = productSkuR.getData();

        //2：根据商品id获取商品对象
        //商品对象
        Long productId = productSku.getProductId();
        R<Product> productR = remoteProductService.getProduct(productId, SecurityConstants.INNER);
        Product product = productR.getData();
        //封装轮播图
        String sliderUrls = product.getSliderUrls();
        List<String> productSliderUrlsList = Arrays.asList(sliderUrls.split(","));

        //3：获取商品最新价格
        R<SkuPrice> skuPriceR = remoteProductService.getSkuPrice(productId, SecurityConstants.INNER);
        SkuPrice skuPrice = skuPriceR.getData();

        //4：获取商品详情图片列表 得先获取商品详情
        R<ProductDetails> productDetailsR = remoteProductService.getProductDetails(productId, SecurityConstants.INNER);
        ProductDetails productDetails = productDetailsR.getData();

        String imageUrls = productDetails.getImageUrls();
        List<String> imageUrlsList = Arrays.asList(imageUrls.split(","));

        //5：获取sku对应商品所有规格
        R<Map<String, Long>> skuSpecValue = remoteProductService.getSkuSpecValue(productId, SecurityConstants.INNER);
        Map<String, Long> skuSpecValueMap = skuSpecValue.getData();

        //6：获取库存信息
        R<SkuStockVo> skuStockR = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
        SkuStockVo skuStockVo = skuStockR.getData();

        itemVo.setProductSku(productSku);
        itemVo.setProduct(product);
        itemVo.setSliderUrlList(productSliderUrlsList);
        itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        itemVo.setSkuPrice(skuPrice);
        itemVo.setDetailsImageUrlList(imageUrlsList);
        itemVo.setSkuSpecValueMap(skuSpecValueMap);
        itemVo.setSkuStockVo(skuStockVo);
        return itemVo;

    }
}
